/*	MSG.java
 *
 *	Copyright (c) Manateem Challenge Group, Zaia Group, and contributors 2010
 *
 *	This file is part of MSGSwingSet ('this project').
 *	
 *	This project is free software: you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the Free
 *	Software Foundation, either version 3 of the License, or (at your option)
 *	any later version.
 *	
 *	This project is distributed in the hope that it will be useful, but WITHOUT
 *	ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *	FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 *	more details.
 *	
 *	You should have received a copy of the GNU General Public License along with
 *	this project. If not, see <http://www.gnu.org/licenses/>.
 */
package edu.bu.manateem.msg;

// Imports
import java.util.ArrayList;

import edu.bu.manateem.msg.analysis.Analysis;
import edu.bu.manateem.msg.analysis.TargetedAbundanceAnalysis;
import edu.bu.manateem.msg.analysis.io.AnalysisArgs;
import edu.bu.manateem.msg.analysis.io.TargetedAbundanceAnalysisArgs;
import edu.bu.manateem.msg.gui.MainFrame;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *	<p>
 *	MSG ToolSuite main invoker. Starts the MSG ToolSuite application (default is
 *	to use the GUI, though can be disabled).
 *	</p>
 *
 *
 *	@author
 *		Christopher Jacobs
 *
 *	@version
 *		1.0
 *
 *	@changelog
 *		May 12, 2010	- Version 1.0.
 */
public class Manatee
{
	/*
	 * TYPES
	 */

	private enum UI
	{
		GUI,
		CL
	}
	
	/*
	 * CONSTANTS
	 */

	// Logging
	private static final Logger LOG = LoggerFactory.getLogger(Manatee.class);
	
	// Usage
	private static final String USAGE = "";
	
	/*
	 * FIELDS
	 */

	private static UI ui = UI.GUI;
	private static AnalysisArgs analysisArgs;
	private static Analysis analysisInst;
	
	/*
	 * CONSTRUCTORS
	 */

	/*
	 * INITIALIZERS
	 */

	/*
	 * METHODS
	 */

	/**
	 *	<p>
	 *	Entry-point method.
	 *	</p>
	 *	Usage:
	 *	<pre>> MSG</pre>
	 *	<p>
	 *	Starts the MSG ToolSuite application.
	 *	</p>
	 */
	public static void main(String[] args)
	{
		if (!Manatee.parse(args))
		{
			System.out.println(Manatee.USAGE);
		}
		
		Manatee.LOG.info("start MSG...");
		switch (Manatee.ui)
		{
			case GUI:
				Manatee.runGUI();
				break;
			case CL:
				Manatee.runCL(Manatee.analysisInst,Manatee.analysisArgs);
				break;
		}
		
	}
	
	private static boolean parse(String[] args)
	{
		ArrayList<String> argList = new ArrayList<String>();
		for (String arg : args)
		{
			argList.add(arg);
		}
		if (argList.contains("--nogui"))
		{
			argList.remove("--nogui");
			Manatee.ui = UI.CL;
		}
		if (argList.contains("--analysis=targeted"))
		{
			System.out.println("HERE");
			Manatee.analysisArgs = new TargetedAbundanceAnalysisArgs();
			Manatee.analysisInst = new TargetedAbundanceAnalysis();
			argList.remove("--analysis=targeted");
			if (Manatee.analysisArgs.parse(argList))
			{
				return true;
			}
		}
		return false;
	}
	
	private static void runCL(Analysis analysis,AnalysisArgs args)
	{
		analysis.runAnalysis(args);
	}
	
	private static void runGUI()
	{
		javax.swing.SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				Manatee.LOG.debug("build main frame");
				new MainFrame().setVisible(true);
				// MSG.LOG.info("build DebugDialog");
				// new DebugDialog().setVisible(true);
			}
		});
	}
}
